package com.lq.employmentmanage.controller;

import com.lq.employmentmanage.common.CommonResult;
import com.lq.employmentmanage.entity.EmploymentInfo;
import com.lq.employmentmanage.service.EmploymentInfoService;
import java.util.Date;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.UUID;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value = "/employmentManagement",name = "就业")
public class EmploymentInfoController {

  private final EmploymentInfoService employmentInfoService;

  public EmploymentInfoController(
      EmploymentInfoService employmentInfoService) {
    this.employmentInfoService = employmentInfoService;
  }

  @GetMapping(value = "index",name = "跳转就业页面")
  public ModelAndView index() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("system/employmentinfo/employmentinfo");
    return modelAndView;
  }

  @GetMapping(value = "/managementInfo")
  public ModelAndView managementInfo() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("system/employmentinfo/employmentinfo");
    return modelAndView;
  }

  @ResponseBody
  @PostMapping(value = "/getEmploymentList",name = "列表")
  public CommonResult<List<EmploymentInfo>> getAllInfo(EmploymentInfo employmentInfo,
      @RequestParam("limit") int pageSize, @RequestParam("page") int pageNum) {
    List<EmploymentInfo> infoList = employmentInfoService
        .getAllEmploymentInfo(employmentInfo, pageNum, pageSize);
    CommonResult<List<EmploymentInfo>> rtInfoResult = CommonResult
        .generateSuccessResult(infoList.size(), infoList);
    return rtInfoResult;
  }

  @ResponseBody
  @PostMapping(value = "/getEmploymentInfo",name = "详情")
  public CommonResult<List<EmploymentInfo>> getEmploymentInfo(EmploymentInfo info,
      @RequestParam("limit") int pageSize, @RequestParam("page") int pageNum) {
    List<EmploymentInfo> infoList = employmentInfoService
        .getEmploymentInfo(info, pageNum, pageSize);
    CommonResult<List<EmploymentInfo>> rtInfoResult = CommonResult
        .generateSuccessResult(infoList.size(), infoList);

    return rtInfoResult;
  }

  @ResponseBody
  @PostMapping(value = "/addEmploymentInfo",name = "新增")
  public CommonResult<Integer> addEmploymentInfo(EmploymentInfo info) {
    info.setInformationId(UUID.randomUUID().toString());
    info.setEmploymentTime(new Date());
    employmentInfoService.addEmploymentInfo(info);
    return CommonResult.generateSuccessResult(1, 1);
  }

  @ResponseBody
  @PostMapping(value = "/updateEmploymentInfo",name = "修改")
  public CommonResult<Integer> updateEmploymentInfo(EmploymentInfo info) {
    employmentInfoService.updateEmploymentInfo(info);
    return CommonResult.generateSuccessResult(1, 1);
  }

  @PostMapping(value = "/delEmployment/{infoId}",name = "删除")
  public CommonResult<Integer> delInfo(@PathVariable("infoId") String infoId) {
    employmentInfoService.deleteEmploymentInfo(infoId);
    return CommonResult.generateSuccessResult(1, 1);
  }

  @ResponseBody
  @PostMapping(value = "/statistic/{fieldName}",name = "统计")
  public CommonResult<List<Map<String, String>>> getStatisticData(
      @PathVariable("fieldName") String fieldName,
      @RequestParam("limit") int pageSize, @RequestParam("page") int pageNum) {
    List<Map<String, String>> statisticResult = employmentInfoService
        .getStudentCount(fieldName, pageNum, pageSize);
    return CommonResult.generateSuccessResult(statisticResult.size(), statisticResult);
  }
}
